package org.come.mouslisten;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.math.BigDecimal;
import java.util.Map;

import com.tool.Document.RichDocument;
import com.tool.Stall.AssetUpdate;
import com.tool.image.ImageMixDeal;
import com.tool.role.RoleData;

import org.come.Frame.PetSkillsJframe;
import org.come.Frame.XYJframe;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.PetSkillsJpanel;
import org.come.bean.ConfigureBean;
import org.come.bean.Skill;
import org.come.control.AssetControl;
import org.come.entity.RoleSummoning;
import org.come.model.Configure;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.*;

import javax.swing.*;
import javax.swing.text.BadLocationException;

public class ShowPetSkillsMouslisten implements MouseListener {
    private int skillwl;
    private Skill skill;

    private int index;
    private PetSkillsJpanel petSkillsJpanel;
    private int[] xy;

    public ShowPetSkillsMouslisten(int index, PetSkillsJpanel petSkillsJpanel, int[] xy) {
        this.petSkillsJpanel = petSkillsJpanel;
        this.xy = xy;
        this.index = index;

    }

    public ShowPetSkillsMouslisten(int index) {
        this.index = index;
    }

    //你干什么去了
    public int getIndex() {
        return index;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // 先判断这个表格是否有召唤兽技能信息
        if (skill != null) {// 有召唤兽技能
            if (e.getButton() == MouseEvent.BUTTON1) {// 左键点击
                if (e.isShiftDown()) {
                    try {
                        JTextField SendMes = ZhuFrame.getZhuJpanel().getSendMes();
                        ((RichDocument) SendMes.getDocument()).insertRich(SendMes.getCaretPosition(), 3,
                                UserMessUntil.getChosePetMes().getSid(), "["
                                        + UserMessUntil.getChosePetMes().getSummoningname() + "]", "G", null);
                    } catch (BadLocationException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
                // 判断鼠标是否是加锁图标
                if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE10)) {

                    ConfigureBean allConfigure = UserMessUntil.getConfigureBean();
                    Map<BigDecimal, Configure> item = allConfigure.getAllConfigure();
                    Configure configure7 = item.get(new BigDecimal(7));

                    MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
                    // 判断是否已加锁
                    boolean f = false;
                    if (UserMessUntil.getChosePetMes().getPetSkilllock() == null) {
                        UserMessUntil.getChosePetMes().setPetSkilllock("");
                    } else {
                        String[] lcnum = UserMessUntil.getChosePetMes().getPetSkilllock().split("\\|");

                        if ("开".equals(configure7.getLyssx())) {
                            if (lcnum.length > 3) {
                                ZhuFrame.getZhuJpanel().addPrompt("最多只能锁定4个技能！");
                                return;
                            }
                        } else if ("关".equals(configure7.getLyssx())) {
                            if (lcnum.length > 0) {
                                ZhuFrame.getZhuJpanel().addPrompt("锁定技能功能未开启！");
                                return;
                            }
                        }
                    }
                    if (PetSkillsJpanel.showPetSkills != null && PetSkillsJpanel.showPetSkills.length > 0) {
                        for (int i = 0; i < PetSkillsJpanel.showPetSkills.length; i++) {
                            if (PetSkillsJpanel.showPetSkills[i].getSkill() != null) {
                                if (skill.getSkillid().equals(PetSkillsJpanel.showPetSkills[i].getSkill().getSkillid())) {
                                    f = true;
                                }
                            }
                        }
                    }
                    if (f) {
                        if (UserMessUntil.getChosePetMes().getPetSkilllock() != null && UserMessUntil.getChosePetMes().getPetSkilllock().contains(skill.getSkillid())) {
                            ZhuFrame.getZhuJpanel().addPrompt("该技能已加锁");
                        } else {
                            AssetUpdate assetUpdate = new AssetUpdate();
                            if(UserMessUntil.getChosePetMes().getPetSkilllock()!=null && UserMessUntil.getChosePetMes().getPetSkilllock()!="") {
                                String[] num = UserMessUntil.getChosePetMes().getPetSkilllock().split("\\|");
                                //锁定消耗仙玉
                                int lockNum = num.length+1;
                                int xyh = 0;
                                if(lockNum == 1){
                                    xyh = 100;
                                }else if(lockNum == 2){
                                    xyh = 200;
                                }else if(lockNum == 3){
                                    xyh = 300;
                                }else if(lockNum == 4){
                                    xyh = 400;
                                }

                                if(Util.isxy(Long.parseLong(xyh+""))) {
                                    String sendMes = Agreement.updateXianyu("xianyu="+(num.length+1)+"");
                                    SendMessageUntil.toServer(sendMes);
                                }else {
                                    ZhuFrame.getZhuJpanel().addPrompt("仙玉不足无法锁定！");
                                    return;
                                }
                            }else {

                                if(Util.isxy(Long.parseLong(100+""))) {
                                    String sendMes = Agreement.updateXianyu("xianyu="+(1)+"");
                                    SendMessageUntil.toServer(sendMes);
                                }else {
                                    ZhuFrame.getZhuJpanel().addPrompt("仙玉不足无法锁定！");
                                    return;
                                }
//								String sendMes = Agreement.updateXianyu("xianyu="+1+"");
//								SendMessageUntil.toServer(sendMes);
//								UserMessUntil.getChosePetMes().setPetSkilllock("");
                            }
//                            //system.out.println(UserMessUntil.getChosePetMes().getPetSkilllock());
                            UserMessUntil.getChosePetMes().setPetSkilllock(UserMessUntil.getChosePetMes().getPetSkilllock()+skill.getSkillid()+"|");
                            // 发送服务器
                            try {
                                SendRoleAndRolesummingUntil.sendRoleSumming(UserMessUntil.getChosePetMes());
                            } catch (Exception e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }
                            ZhuFrame.getZhuJpanel().addPrompt(" 加锁成功！");
                        }
                    } else {
                        ZhuFrame.getZhuJpanel().addPrompt("只有学习的技能才可以锁定！");
                    }
                }
                // 判断鼠标是否是加锁图标
                if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE11)) {
                    // 判断是否已加锁
                    if (UserMessUntil.getChosePetMes().getPetSkilllock().contains(skill.getSkillid())) {
                        MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
                        String lists = "";
                        if (UserMessUntil.getChosePetMes().getPetSkilllock() != null && UserMessUntil.getChosePetMes().getPetSkilllock() != "") {
                            String[] list = UserMessUntil.getChosePetMes().getPetSkilllock().split("\\|");
                            if (list != null && list.length > 0) {
                                for (String id : list) {
                                    if (!skill.getSkillid().equals(id)) {
                                        lists = lists + id + "|";
                                    }
                                }
                            }
                        }
                        UserMessUntil.getChosePetMes().setPetSkilllock(lists);
                        // 发送服务器
                        try {
                            SendRoleAndRolesummingUntil.sendRoleSumming(UserMessUntil.getChosePetMes());
                        } catch (Exception e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        ZhuFrame.getZhuJpanel().addPrompt("解锁成功");
                    }
                }
            }
            PetSkillsJframe.getPetSkillsJframe().getPetSkillsJpanel().skillmsg(skill, skillwl);
            PetSkillsJframe.getPetSkillsJframe().getPetSkillsJpanel().setPetskillID(skill.getSkillid());
            PetSkillsJframe.getPetSkillsJframe().getPetSkillsJpanel().setPetskillNO(index);
            /*兑换版本心猿*/
            if ("200193".equals(UserMessUntil.getChosePetMes().getSummoningid()) && "25000".equals(skill.getSkillid())) {
                FormsManagement.showForm(688);
            }
            /*心路版本心猿屏蔽中*/
            if (XYJframe.getXYJframe().getXyJpanel().isVisible()&& "一念成圣".equals(skill.getSkillname())){
                RoleSummoning pet = UserMessUntil.getChosePetMes();
                if (!"728".equals(pet.getSummoningskin())) {
                    SendMessageUntil.toServer(Agreement.getAgreement().rolechangeAgreement("OPEN"+"&"+pet.getSid()));
                }
            }

        } else {
            PetSkillsJframe.getPetSkillsJframe().getPetSkillsJpanel().getBox().setText(null);
        }
        if (petSkillsJpanel != null) {
            petSkillsJpanel.getEffect().setBounds(xy[0], xy[1], 50, 50);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
//		MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
        // TODO Auto-generated method stub
        if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE1)) {
            MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE12);
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
//		MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
        // TODO Auto-generated method stub
        if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE12)) {
            MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
        }
    }


    public Skill getSkill() {
        return skill;
    }

    public void setSkill(Skill skill) {
        this.skill = skill;
    }

    public int getSkillwl() {
        return skillwl;
    }

    public void setSkillwl(int skillwl) {
        this.skillwl = skillwl;
    }

}
